# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import pymongo
from itemadapter import ItemAdapter
from icecream import ic

class FfPipeline:
    def __init__(self,db,client):
        self.client = pymongo.MongoClient()
        self.db = self.client[db]
    @classmethod
    def from_crawler(cls,crawler,*args,**kwargs):
        obj = cls(
            db = crawler.settings.get('DB'),
            client = crawler.settings.get('MONGOIP')
        )
        return obj

    # mongodb 插入数据
    def process_item(self, item, spider):
        # ic(type(item),item)
        items = dict(item)
        if isinstance(items,dict):
            self.db['ff'].insert(items)
            return item
        else:
            return '数据格式有误'

